__author__ = 'Administrator'

import sys
import os.path
from ExcelFileConfig import *
from Config import *
from enum_define import *
from SheetLoader import *
from SheetParser import *
from LuaCodeGenerator import *


def excel_to_game(file_path, sheet_name):
    # 加载Excel文件
    loader = SheetLoader()
    if not loader.load(file_path, sheet_name):
        return
    # Excel表解析器
    parser = SheetParser()
    if not parser.parse(loader):
        return

    for i in parser.get_fields():
        print(i)

    # print(parser.get_data())
    # 生成代码（Py, Cpp, CS,lua)
    lua_code_generator = LuaCodeGenerator(parser)
    lua_code_generator.generate_field()
    lua_code_generator.generate_data()


def convert_all_excels_to_game():
    lua_code_generator = LuaCodeGenerator()
    lua_code_generator.pre_generate_field()
    for excel_name, sheet_name_list in excel_list.items():
        file_path = os.path.join(excel_folder, excel_name)
        for sheet_name in sheet_name_list:
            excel_to_game(file_path, sheet_name)
    lua_code_generator.post_generate_field()


def convert_enums():
    with open(os.path.join(output_lua_folder, 'Resources/Table', 'ENUM_DEFINE.lua'), 'wt', encoding='utf-8') as file:
        for _, enum_info in enums.items():
            file.write("'''\n")
            file.write('%s\n' % enum_info['desc'])
            file.write("'''\n")
            for enum_str, enum_detail in enum_info['values'].items():
                file.write('%-10s=  %-20s---%s\n' % (enum_str, enum_detail[0], enum_detail[1]))
            file.write('\n')



if __name__ == '__main__':
    args = len(sys.argv)

    if args != 1 and args != 2:
        print('============================================================')
        print('Usage:')
        print('    Python Excel2Game.py excelName   导单个文件')
        print('    Python Excel2Game.py             导所有文件')
        print('============================================================')

        sys.exit(0)

    if args == 1:
        # 转换enum
        convert_enums()
        # 转换所有Excel到脚本
        convert_all_excels_to_game()
    else:
        # 转换特定的Excel到脚本
        excel_to_game(sys.argv[1], sys.argv[2])





